package models

import (
	"fitness-srv/basic/config"
	"gorm.io/gorm"
)

// 器材表
type Equipment struct {
	gorm.Model
	UserId   int    `json:"user_id" gorm:"type:int;comment:'用户id'"`
	Name     string `json:"name" gorm:"type:varchar(50);comment:'名称'"`
	Class    string `json:"class" gorm:"type:varchar(30);comment:'分类'"`
	Desc     string `json:"desc" gorm:"type:varchar(100);comment:'功能'"`
	Duration string `json:"duration" gorm:"type:varchar(30);comment:'时长'"`
}

func (e *Equipment) TableName() string {
	return "equipment"
}

func (e *Equipment) AddEquipment() error {
	return config.DB.Create(&Equipment{}).Error
}

func (e *Equipment) GetEquipment(id int64) error {
	return config.DB.Model(&Equipment{}).Where("id = ?", id).Limit(1).Find(&e).Error
}

func (e *Equipment) GetEquipmentDesc(class, desc string) (equipmentList []*Equipment, err error) {
	db := config.DB.Model(&Equipment{})
	if class != "" {
		db = db.Where("class like ?", "%"+class+"%")
	}
	if desc != "" {
		db = db.Where("desc like ?", "%"+desc+"%")
	}
	db.Find(&equipmentList)
	return equipmentList, err
}
